package www.houdoudou.com.common;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class TelnumVerifyManagement {

	 public static JSONObject  manage(String jsonData){
	  	   
	   	  JSONObject result = new JSONObject();
	   		JSONObject jsonfrom= JSONObject.fromObject(jsonData);				
	   		//判断请求的功能d
	   		int FunctionCode=jsonfrom.getInt("FunctionCode");
	   		   PreparedStatement pstm=null; 
	   		   Statement  stm=null;
	   		   ResultSet rs=null; 	
	   		   String CurrentConnUser=CommonMathFunction.genRandomNum(10);
	   		   Connection conn=ConnFactory.getCurrentConnection(CurrentConnUser);
	   		   switch(FunctionCode){
	   		   //新增
	   		  case 4:{
				   try {	
					String sql = "insert  into Tb_Verify_Telnum (Telnum,Verify_Code) values (?,?)";
	              	pstm=conn.prepareStatement(sql);
					pstm.setString(1, jsonfrom.getString("Telnum"));
					pstm.setString(2, "1234");	
					pstm.executeUpdate();
					result.put("ActionStatus", true);
					result.put("ErrorInfo", "验证码获取成功");
					
				  }catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						result.put("ActionStatus", false);
						result.put("ErrorInfo", "验证码获取失败失败");
					}finally{
						ConnFactory.closeConn(CurrentConnUser, pstm, stm, rs);
					}
				   
			                 break;} //end of case 4
	   		//删除
	   		  case 3:{
	   			String Telnum=jsonfrom.getString("Telnum");	
				   try {	
					String sql="delete from Tb_Verify_Telnum  where Telnum="+Telnum+""; 		   
		            stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);  							           
	           		 stm.executeUpdate(sql);  	
	    				
							result.put("ActionStatus", true);
							result.put("ErrorInfo", "验证码删除成功");				
				  }catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						result.put("ActionStatus", false);
						result.put("ErrorInfo", "验证码删除失败失败");
					}finally{
						ConnFactory.closeConn(CurrentConnUser, pstm, stm, rs);
					}
				   
			                 break;} //end of case 4
	   		
	   		   
	     case 5:{
		  try {
			 	String Telnum=jsonfrom.getString("Telnum");	 
			   String sql = "select * from Tb_Verify_Telnum  where Telnum=  "+Telnum+"";  
			   System.out.println(sql);
				stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
				rs= stm.executeQuery(sql);
				JSONArray Verify_Codes = new JSONArray();
				while(rs.next()){
					JSONObject code= new JSONObject();
					code.put("Verify_Code", rs.getString("Verify_Code"));
					Verify_Codes.add(code);
				}
				result.put("ActionStatus", true);
				result.put("ErrorInfo", "查询验证码成功");
				result.put("Verify_Codes",  Verify_Codes);
				
		  }catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				result.put("ActionStatus", false);
				result.put("ErrorInfo", "验证码查询失败");
			}finally{
				ConnFactory.closeConn(CurrentConnUser, pstm, stm, rs);
			}
		   
	                 break;} //end of case 4 
	     
	   		   }// end of switch functionCode
	   		   
	   		   return result; 
		 }
	
    public static boolean checkVerifyCode(String jsonData){
    	boolean result=false;
    	JSONObject jsonfrom= JSONObject.fromObject(jsonData);			
    	String Verify_Code=jsonfrom.getString("Verify_Code"); 
    	JSONObject innerJSONOVerify=jsonfrom;	
    	innerJSONOVerify.put("FunctionCode", 5);
	      JSONObject innerJSONOVerifyB= TelnumVerifyManagement.manage(innerJSONOVerify.toString());
         if(!innerJSONOVerifyB.getBoolean("ActionStatus")){
      	    result=false;
         }else{
      	   JSONArray Verify_Codes=innerJSONOVerifyB.getJSONArray("Verify_Codes");
      	   for(int i=0;i<Verify_Codes.size();i++){
      		   String Corrent_Verify_Code=Verify_Codes.getJSONObject(i).getString("Verify_Code");
      		   if(Corrent_Verify_Code.equals(Verify_Code)){
      			  result=true;
      			   //删除废弃验证码
      			   JSONObject innerJSONOVerifyDel=jsonfrom;
      			   innerJSONOVerifyDel.put("FunctionCode", 3);
      			   TelnumVerifyManagement.manage(innerJSONOVerifyDel.toString());
      		   }
      	   }
         }
    	return result;
    }
}
